package com.zhibang.sinocare.jcz.controller;





import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import com.zhibang.comnon.domain.Jcz;
import com.zhibang.comnon.domain.SccDrugstore;
import com.zhibang.comnon.result.Result;
import com.zhibang.comnon.result.ResultCode;
import com.zhibang.sinocare.jcz.service.JczService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;


/**
 * @author SeA
 */
@RestController
@CrossOrigin   //解决跨域
@RequestMapping("/user")
@Api(tags = "用户管理模块",description = "对用户进行CRUD操作")
public class JczController {
    //引入service
    @Autowired
    private JczService jczService;

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询用户", notes = "根据id查询用户详细信息", httpMethod = "GET")
    public Result findUser(@PathVariable("id") Integer id) throws Exception{

        //使用mybatis-plus提供的方法，实现CRUD
        Jcz byId = jczService.getById(id);
        return Result.ok(ResultCode.SUCCESS, byId);
    }


    //接口
    //分页查询
    @RequestMapping(value = "find", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户", notes = "根据条件分页查询", httpMethod = "GET")
    public Result findUsers(Integer page, Integer limit, String searchParams) throws Exception {
        QueryWrapper<Jcz> wrapper = new QueryWrapper<>();
        if (searchParams!=null){
            Jcz sccPatient = JSON.parseObject(searchParams, Jcz.class);
            wrapper.like(!sccPatient.getName().equals("")&& sccPatient.getName()!=null,"name",sccPatient.getName());
            wrapper.like(!sccPatient.getPhone().equals("")&& sccPatient.getPhone()!=null,"phone",sccPatient.getPhone());
            System.out.println(sccPatient);
        }
        PageHelper.startPage(page,limit);
        List<Jcz> list = jczService.list(wrapper);
        PageInfo<SccDrugstore> info = new PageInfo(list);
        return Result.ok(ResultCode.SUCCESS,info.getTotal(),info.getList());
    }





    //需求：根据id删除用户

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据id删除用户", notes = "根据id删除用户信息", httpMethod = "DELETE")
    public Result deleteById(@PathVariable("id") Integer id) throws Exception {


//        int i = jczService.deleteById(id);
        boolean i = jczService.removeById(id);

        if (i) {
            return Result.ok(ResultCode.SUCCESS, i);
        } else {
            return Result.ok(ResultCode.FAIL, null);
        }


    }




    /**
     * 添加用户
     * @param jcz
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    public Result addUsers(@RequestBody Jcz jcz) throws  Exception{
        System.out.println(jcz);
        boolean i = jczService.save(jcz);

        if (i){
            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return  Result.ok(ResultCode.FAIL,null);
        }


    }

    //    //需求：批量删除
    @RequestMapping(value = "batch", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result batchDeleteUsers(String str) throws Exception {



        String[] split = str.substring(1).split(",");
        List<String> list = new ArrayList<>(split.length);
        for (String s : split) {
            list.add(s);
        }
        boolean i = jczService.removeByIds(list);

        if (i) {
            return Result.ok(ResultCode.SUCCESS, i);
        } else {
            return Result.ok(ResultCode.FAIL, null);
        }

    }


}







